package com.team.arms.bingyi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.arms.bingyi.domain.BingyiRegistInfo;
import com.team.arms.bingyi.domain.vo.ConsAllVo;
import com.team.arms.bingyi.domain.vo.ConscriptVo;
import com.team.arms.bingyi.domain.vo.ExaminepassVo;
import com.team.arms.bingyi.mapper.BingyiRegistInfoMapper;
import com.team.arms.bingyiInfo.entity.BingyiInfo;
import com.team.arms.bingyiInfo.mapper.BingyiInfoMapper;
import com.team.arms.common.Result;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.political.domain.PoliReviewInfo;
import com.team.arms.political.mapper.PoliReviewInfoMapper;
import com.team.arms.political.service.PoliReviewInfoService;
import com.team.arms.tijian.mapper.SysHealthExaminationMapper;
import com.team.arms.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author huazige
 * @Date 2022/6/16 17:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/conscript/examinepass")
@Api(tags="征兵管理-初检通过")
@CrossOrigin
public class ExaminepassController {

    @Autowired
    BingyiRegistInfoMapper bingyiRegistInfoMapper;
    @Autowired
    BingyiInfoMapper bingyiInfoMapper;
    @Autowired
    PoliReviewInfoMapper poliReviewInfoMapper;
    @Autowired
    SysHealthExaminationMapper sysHealthExaminationMapper;
    @Autowired
    PoliReviewInfoService poliReviewInfoService;

    /**
     * 查询所有征兵管理
     */
//    @ApiOperation("获取所有初检通过")
////    @Log(title = "政审信息", businessType = BusinessType.)
//    @GetMapping(value = "/get")
//    public AjaxResult selectpltc() {
//
//        List<ExaminepassVo> list = bingyiRegistInfoMapper.selectexaminepass();
//        return AjaxResult.success("查询成功",list) ;
//
//    }
    @ApiOperation("分页显示初检通过")
    @GetMapping(value = "/listmsg/{pageNum}/{pageSize}")
    public Result selectMsg(
            @ApiParam("当前页数")       @PathVariable("pageNum")Integer pageNum,
            @ApiParam("每页的数据量")       @PathVariable("pageSize")Integer pageSize

    ){
        List<ExaminepassVo> list = bingyiRegistInfoMapper.selectexaminepass();
        //转换效率的问题,直接sql查询返回这个类型效率相对高一些
        Page page = PageUtil.list2Page(list, pageNum, pageSize);
        return page !=null ? Result.success("分页成功").data(page): Result.error("分页失败");
    }

    @ApiOperation("下拉框检索体检通过信息")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@RequestParam(value = "查询的字段") String field , @RequestParam(value = "查询的内容")String content){
        return AjaxResult.success(bingyiRegistInfoMapper.selectexaminepassByFiled(field,content));
    }





    @Transactional
    @ApiOperation("单个+批量删除初检通过")
    @DeleteMapping("/deletelist")
    public AjaxResult deleteInformation(@RequestParam("poliInfoId")List<String> deletelist ){
        for ( String id:deletelist
        ) {
            QueryWrapper<PoliReviewInfo> wrapper = new QueryWrapper();
            wrapper.eq("bingyi_info_id",id);
            poliReviewInfoMapper.delete(wrapper);

            QueryWrapper<BingyiInfo> wrapper2 = new QueryWrapper();
            wrapper2.eq("bingyi_info_id",id);
            bingyiInfoMapper.delete(wrapper2);

            QueryWrapper<BingyiRegistInfo> wrapper3 = new QueryWrapper();
            wrapper3.eq("bingyi_info_id",id);
            bingyiRegistInfoMapper.delete(wrapper3);

            QueryWrapper<entity.SysHealthExamination> wrapper4 = new QueryWrapper();
            wrapper4.eq("bingyi_info_id",id);
            sysHealthExaminationMapper.delete(wrapper4);
        }


        return  AjaxResult.success("删除成功") ;

    }

    @ApiOperation("导出所有初检通过信息")
    @GetMapping("/export")
    public void export(HttpServletResponse response)
    {

        List<ExaminepassVo> list = bingyiRegistInfoMapper.selectexaminepass();
        poliReviewInfoService.exportpoli(response,list, ConscriptVo.class,"应征人员表");


    }

    @ApiOperation("修改初检通过信息")
    @PutMapping("/update")
    public AjaxResult updateInformation(
            @RequestParam(value = "bingyiInfoId")String bingyiInfoId,
            @RequestBody ExaminepassVo examinepassVo){

        Boolean b = bingyiRegistInfoMapper.updateexaminepass(bingyiInfoId, examinepassVo);
        return b ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");

    }

    @ApiOperation("申请上站")
    @PutMapping("/apply")
    public AjaxResult pass(
            @RequestParam(value = "bingyiInfoId")String bingyiInfoId){
        QueryWrapper<BingyiRegistInfo> bingyi = new QueryWrapper<>();
        bingyi.eq("bingyi_info_id", bingyiInfoId);
        BingyiRegistInfo bingyiRegistInfo = bingyiRegistInfoMapper.selectOne(bingyi);
        bingyiRegistInfo.setUpState("待市武装部确认");
        int update = bingyiRegistInfoMapper.update(bingyiRegistInfo, bingyi);
        return update>0 ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");

    }
    @ApiOperation("获取某个人的详情内容")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @GetMapping(value = "/getall/{bingyiInfoId}}")
    public AjaxResult selectall(
            @PathVariable("bingyiInfoId")String bingyiInfoId
    ) {

        List<ConsAllVo> list = bingyiRegistInfoMapper.selectall(bingyiInfoId);
        return AjaxResult.success("查询成功",list) ;

    }


}
